Skip to content

feat(views): load fee history for quiet wallets in milliseconds - #1498

Merged
MicBun merged 2 commits into
mainfrom
perf/fee-history-index
Nov 25, 2026
Merged

MicBun merged 2 commits into
mainfrom
perf/fee-history-index

Conversation

@MicBun

@MicBun MicBun commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fee history for a wallet with few fee rows reads the whole transaction_events table and can run past the RPC timeout. list_transaction_fees in received and both mode, and get_last_transactions with a wallet, OR the caller test with the fee recipient test. No index answers that filter, so Postgres reads transaction_events_block_idx from the newest row and tests every row until it has a page. A wallet with fewer rows than the page asks for makes it read every row.

Migration 065 redefines both actions and adds one index.

  • transaction_events_fee_recipient_idx on (fee_recipient, block_height, tx_id), the same shape as transaction_events_caller_idx.
  • Each wallet test is its own branch. A branch reads the keys of the wallet's events from one index, newest first, and stops at limit + offset. paid reads the caller index, received the fee recipient index, and both the UNION of the two. Only the page's events are then read by tx_id.
  • Parameters, columns, row order, paging and distribution rows stay as they were. Where limit + offset passes the INT maximum, the branches read every key instead, so a huge offset still returns an empty page as before.
  • The filters no longer look for the wallet in transaction_event_distributions. Its only writer, record_transaction_event (027), writes at most one row per event, to the event's fee_recipient, so that test never matched an extra event. The ledger test that writes a split row by hand now expects that row's second recipient to have no fee history.

The change is SQL only: two actions and one index, applied by the migration. No new binary.

Measurements

I copied the mainnet fee events into a local Postgres 17 with the node's schema: 3,980,764 events, plus 1,613,278 distribution rows written the way record_transaction_event writes them, 2.5 GB with indexes. I ran the SQL that kwil generates for the old and new actions, with custom plans as kwil gets them, and took the median of three runs after one EXPLAIN (ANALYZE, BUFFERS).

list_transaction_fees, 20 rows, first page / offset 10,000:

wallet mode old new
no fee rows received 700 / 780 ms 0.6 / 0.5 ms
no fee rows both 805 / 769 ms 0.5 / 0.4 ms
3 fee rows paid 0.3 / 0.3 ms 0.5 / 0.4 ms
3 fee rows received 762 / 765 ms 0.4 / 0.4 ms
3 fee rows both 792 / 798 ms 0.6 / 0.4 ms
busiest caller paid 107 / 123 ms 0.7 / 10 ms
busiest caller received 708 / 722 ms 0.6 / 0.6 ms
busiest caller both 134 / 143 ms 0.9 / 10 ms
leader paid 0.5 / 2.5 ms 0.9 / 2.1 ms
leader received 0.7 / 5.9 ms 0.8 / 8.0 ms
leader both 0.6 / 6.7 ms 0.9 / 13 ms

get_last_transactions, 6 / 100 rows:

wallet old new
none given 0.4 / 1.0 ms 0.5 / 1.3 ms
no fee rows 730 / 817 ms 0.4 / 0.5 ms
3 fee rows 766 / 731 ms 0.7 / 0.6 ms
busiest caller 114 / 122 ms 0.5 / 1.5 ms
leader 0.4 / 0.9 ms 0.6 / 1.6 ms
  • The busiest caller holds 1.73M events, the newest of them 830k blocks below the tip; the leader received 1.61M.
  • Every old plan above that takes more than 100 ms walks transaction_events_block_idx backward with the wallet as a filter, reading about 189,700 buffers for a full walk. No new plan reads that index for a wallet: each branch is an index only scan of the caller or fee recipient index, with no heap fetches.
  • The leader's deep pages are a few ms slower, since a branch reads limit + offset keys before the page is cut. At offset 100,000 in both mode the leader takes 85 ms (old 55 ms) and the busiest caller 103 ms (old 221 ms).
  • The index only scans rely on the visibility map. With 25% or fewer of the table's pages marked all visible, Postgres planned the busiest caller's branch on the block index again; from 30% up it kept the caller index.
  • Old and new returned the same rows in the same order in every case above.
  • CREATE INDEX on this data took 3.4 s with two parallel workers and 4.7 to 5.4 s with none. The index is 506 MB.

Checks

  • go test -p 1 -timeout 60m -count=1 -tags=kwiltest ./tests/streams -run 'TestTransactionEventsLedger$|TestTransactionIDTracking$|TestTrimTransactionEvents$|TestTrimTransactionEventsE2E$|TestTransactionFeeHistoryReads$' passes.
  • TestTransactionFeeHistoryReads installs the 027 and 010 definitions under other names and compares every row with the new actions over 7 wallets, 5 modes and 13 pages (three with offsets near the INT maximum), and 9 wallets and 7 limits for get_last_transactions. It also checks the results against the dataset and that pages of 4 read back the full list. With the branch limit set to limit instead of limit + offset, it fails.
  • go test -p 1 -count=1 -tags=kwiltest on extensions/tn_utils, tn_digest, tn_settlement and tn_attestation, which seed every migration, passes.
  • golangci-lint v2.10.1 on ./tests/streams/ reports 0 issues.

Deploy

Apply internal/migrations/065-transaction-fee-history-reads.sql on testnet, then mainnet. The index is built while the migration runs.

resolves: https://github.com/trufnetwork/indexer/issues/241

Summary by CodeRabbit

  • New Features
    • Added transaction fee history views for wallet activity, with options to see fees paid, received, or both, and controls for paging through results.
    • Added a latest-transactions view that can show recent activity overall or transactions associated with a wallet, including activity where the wallet received fees.

@MicBun MicBun self-assigned this Nov 24, 2026
@holdex

holdex Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Time Submission Status

Member # Time Running Total Status Last Update
MicBun 1h 30min ✅ Submitted Nov 25, 2026, 2:50 AM

Submit or update total time with:

@holdex pr submit-time 2h

Add time on top of previous submission with:

@holdex pr add-time 1h30m

See available commands to help comply with our Guidelines.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Migration 065 adds wallet-specific fee-history queries and a fee-recipient index. The queries support transaction ordering and pagination. Tests compare the updated actions with prior definitions and check recipient-related results.

Changes: Transaction Fee History

Layer / File(s) Summary
Wallet fee listing
internal/migrations/065-transaction-fee-history-reads.sql
Adds an index on fee recipient, block height, and transaction ID. Defines list_transaction_fees with paid, received, and combined modes, input validation, and pagination.
Latest transaction lookup
internal/migrations/065-transaction-fee-history-reads.sql
Updates get_last_transactions to return the newest events overall or events matching a wallet as caller or fee recipient.
Read behavior tests
tests/streams/transaction_fee_history_test.go, tests/streams/transaction_events_ledger_test.go
Adds integration coverage for ordering, pagination, and fee-recipient cases. Updates ledger expectations so a bonus recipient has no fee-history or transaction-history rows.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Suggested reviewers: outerlook

Merge Risk: Minimal · up to 44783

The migration rewrites the wallet fee-history and latest-transaction reads to use indexes while keeping results and paging unchanged. No concrete defect was established, so it looks safe to merge. Validate the performance claims on testnet before mainnet, as the author planned.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "feat(views): load fee history for quiet wallets in milliseconds" matches the main change. The PR replaces the list_transaction_fees and get_last_transactions views with indexed reads and ad…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "feat(views): load fee history for quiet wallets in milliseconds" matches the main change. The PR replaces the list_transaction_fees and get_last_transactions views with indexed reads and adds a fee-recipient index for wallet-filtered queries. The objectives say the goal is millisecond-scale fee history for quiet wallets. The title names the performance goal and the affected area. The title uses "feat" while the PR objectives use "perf" as the prefix, which is a minor mismatch and does not change the meaning. The title also does not mention the removal of distribution-recipient matching, but the title does not need to cover every detail.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
Fix failing CI checks

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e8399ab3-4c3c-46b2-a2db-99e9c2fa687c
Commits

Reviewing files that changed from the base of the PR and between 892b536 and 4478364.

Files selected for processing: 3

  • internal/migrations/065-transaction-fee-history-reads.sql
  • tests/streams/transaction_events_ledger_test.go
  • tests/streams/transaction_fee_history_test.go

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@MicBun MicBun changed the title perf(views): load fee history for quiet wallets in milliseconds feat(views): load fee history for quiet wallets in milliseconds Nov 24, 2026
@MicBun

MicBun commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@holdex pr submit-time 1h30m

@MicBun
MicBun merged commit 9e00868 into main Nov 25, 2026
14 of 15 checks passed
@MicBun
MicBun deleted the perf/fee-history-index branch November 25, 2026 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant